package com.ticket.sass.common.douyin.openapi.enums

import com.google.gson.JsonDeserializationContext
import com.google.gson.JsonDeserializer
import com.google.gson.JsonElement
import com.google.gson.JsonParseException
import java.lang.reflect.Type

class UniversalEnumDeserializer<T : Enum<T>>(private val enumType: Class<T>) : JsonDeserializer<T> {
    override fun deserialize(json: JsonElement, typeOfT: Type, context: JsonDeserializationContext): T {
        val value = json.asInt
        return enumType.enumConstants.firstOrNull { it.ordinal == value }
            ?: throw JsonParseException("Unknown value: $value for enum class: ${enumType.simpleName}")
    }
}